package com.base.mine;

import android.graphics.Bitmap;
import android.text.TextUtils;
import android.view.View;

import com.alibaba.android.arouter.launcher.ARouter;
import com.base.commonlib.base.CBaseMvpFragment;
import com.base.commonlib.bean.StatEntity;
import com.base.commonlib.bean.User;
import com.base.commonlib.event.ImagePath;
import com.base.commonlib.http.RealNetWorkUtil;
import com.base.commonlib.sp.SpUtil;
import com.base.commonlib.utils.GlideUtil;
import com.base.commonlib.utils.RxBus2;
import com.base.mine.config.RouteConfig;
import com.base.mine.databinding.FragmentMineBinding;
import com.base.mine.presenter.MinePresenter;
import com.base.mine.view.MineView;

import io.reactivex.disposables.Disposable;
import io.reactivex.functions.Consumer;

/**
 * Created by weikailiang on 2019/9/19.
 */

public class MineFragment extends CBaseMvpFragment<MinePresenter> implements MineView{

    protected FragmentMineBinding mBinding;

    @Override
    public void init() {
        mBinding = (FragmentMineBinding) mRootBinding;
        mBinding.imgRight.setVisibility(View.VISIBLE);
        mBinding.imgRight.setImageResource(R.mipmap.ic_setting);
        mBinding.title.setText("我的");
    }

    @Override
    public void start() {

        RxBus2.getInstance().post(new StatEntity(1));
        mPresenter = new MinePresenter(this,_mActivity);
        mPresenter.start();

        if (mPresenter.mUser!=null) {
            if (!TextUtils.isEmpty(mPresenter.mUser.getUserName())) {
                mBinding.ivName.setText(mPresenter.mUser.getUserName());
            } else {
                mBinding.ivName.setText("");
            }
            if (!TextUtils.isEmpty(mPresenter.mUser.getOrgName())) {
                if (mPresenter.mUser.getOrgName().equals(mPresenter.mUser.getDeptName())) {
                    mBinding.ivDept.setText(mPresenter.mUser.getOrgName());
                } else {
                    mBinding.ivDept.setText(mPresenter.mUser.getOrgName() + "-" + mPresenter.mUser.getDeptName());
                }
            }
            if (!TextUtils.isEmpty(mPresenter.mUser.getAvatar())){
                GlideUtil.load(mContext,mPresenter.mUser.getAvatar(),mBinding.ivAvatar);
            }
        }
        mBinding.llSchedule.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                ARouter.getInstance().build(RouteConfig.ACTIVITY_VERSION).navigation();
            }
        });

        mBinding.llPartner.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                if (RealNetWorkUtil.TYPE.equals("0")){//吴中城管
                    ARouter.getInstance().build("/mine/partner").navigation();
                }else if (RealNetWorkUtil.TYPE.equals("1")){//昆山城管
                    ARouter.getInstance().build("/mail/ajpartener").navigation();
                }
            }
        });
        mBinding.imgRight.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                ARouter.getInstance().build(RouteConfig.ACTIVITY_SETTER).navigation();
                //ARouter.getInstance().build("/subject/test_type").navigation();
            }
        });
        mBinding.llRaod.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                ARouter.getInstance().build("/road/mineroad").navigation();
            }
        });


        mBinding.llRequest.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                ARouter.getInstance().build("/case/case2add")
                        .withString("ID","ae5e830a8496497a9d00ad494e5ee4d9")
                        .withInt("num",1)
                        .withString("type","5")
                        .withBoolean("isCase",true)
                        .navigation();
            }
        });
       // mBinding.tvVersion.setText(SystemUtil.packageName(mContext)+"");
        mBinding.llAbout.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                ARouter.getInstance().build("/sweb/common")
                        .withInt("key",1)
                        .withString("url",RealNetWorkUtil.HOST_IP+":8585/kaoshi/index.html#/aboutMe")
                        .withString("title","江苏中越科技")
                        .withString("name","测试")
                        .navigation();
            }
        });
        mBinding.llIntegral.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                ARouter.getInstance().build("/sweb/common")
                        .withInt("key",1)
                        .withString("url",RealNetWorkUtil.HOST_IP+":8585/kaoshi/index.html#/myIntegral")
                        .withString("title","江苏中越科技")
                        .withString("name","测试")
                        .navigation();
            }
        });
        mBinding.llIntro.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                ARouter.getInstance().build("/sweb/common")
                        .withInt("key",1)
                        .withString("url",RealNetWorkUtil.HOST_IP+":8585/kaoshi/index.html#/howUse")
                        .withString("title","江苏中越科技")
                        .withString("name","测试")
                        .navigation();
            }
        });


        mBinding.llCenper.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                ARouter.getInstance().build("/mine/accountprivacy").navigation();
            }
        });

        Disposable disposable = RxBus2.getInstance().doSubscribe(User.class, new Consumer<User>() {
            @Override
            public void accept(User user) throws Exception {
                if (!TextUtils.isEmpty(user.getAvatar())){
                    GlideUtil.load(mContext,user.getAvatar(),mBinding.ivAvatar);
                }
            }
        });
        RxBus2.getInstance().addSubscription(this, disposable);
    }




    @Override
    public void onHiddenChanged(boolean hidden) {
        if (!hidden){
            RxBus2.getInstance().post(new StatEntity(1));
        }
    }

    @Override
    protected int getLayoutResource() {
        return R.layout.fragment_mine;
    }

    @Override
    public void onDestroy() {
        super.onDestroy();
    }
}
